package com.java110.mall.cart.bmo.storeCart.impl;

import com.java110.mall.cart.bmo.storeCart.ISaveStoreCartBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreCartDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.intf.cart.IHousekeepingServFeeInnerServiceSMO;
import com.java110.intf.cart.IStoreCartInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.po.storeOrder.StoreCartPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("saveStoreCartBMOImpl")
public class SaveStoreCartBMOImpl implements ISaveStoreCartBMO {

    @Autowired
    private IStoreCartInnerServiceSMO storeCartInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServFeeInnerServiceSMO housekeepingServFeeInnerServiceSMOImpl;

    /**
     * 添加小区信息
     *
     * @param storeCartPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(StoreCartPo storeCartPo) {

        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(storeCartPo.getShopId());
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");

        StoreCartDto storeCartDto = new StoreCartDto();
        storeCartDto.setPersonId(storeCartPo.getPersonId());
        storeCartDto.setValueId(storeCartPo.getValueId());

        List<StoreCartDto> storeCartDtos = storeCartInnerServiceSMOImpl.queryStoreCarts(storeCartDto);

        //todo 不等于空修改个数
        if (!ListUtil.isNull(storeCartDtos)) {
            storeCartPo.setCartId(storeCartDtos.get(0).getCartId());
            int carNum = Integer.parseInt(storeCartDtos.get(0).getCartNum()) + Integer.parseInt(storeCartPo.getCartNum());
            storeCartPo.setCartNum(carNum + "");
            storeCartInnerServiceSMOImpl.updateStoreCart(storeCartPo);
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }


        storeCartPo.setCartId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_cartId));
        storeCartPo.setState(StoreCartDto.STATE_NO_BUY);
        storeCartPo.setStoreId(shopDtos.get(0).getStoreId());
        if (StoreOrderCartDto.CART_TYPE_PRODUCT.equals(storeCartPo.getCartType())) {
            ProductDto productDto = new ProductDto();
            productDto.setShopId(storeCartPo.getShopId());
            productDto.setProductId(storeCartPo.getProductId());
            List<ProductDto> productDtos = productInnerServiceSMOImpl.queryProducts(productDto);
            Assert.listOnlyOne(productDtos, "未包含商品服务");
            storeCartPo.setProdName(productDtos.get(0).getProdName());
            ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
            productSpecValueDto.setProductId(storeCartPo.getProductId());
            productSpecValueDto.setValueId(storeCartPo.getValueId());
            productSpecValueDto.setShopId(storeCartPo.getShopId());
            List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);
            Assert.listOnlyOne(productSpecValueDtos, "规格不存在");
            storeCartPo.setPrice(productSpecValueDtos.get(0).getPrice());
            storeCartPo.setVipPrice(productSpecValueDtos.get(0).getVipPrice());
        } else if (StoreOrderCartDto.CART_TYPE_SERV.equals(storeCartPo.getCartType())) {
            HousekeepingServDto housekeepingServDto = new HousekeepingServDto();
            housekeepingServDto.setShopId(storeCartPo.getShopId());
            housekeepingServDto.setServId(storeCartPo.getProductId());
            List<HousekeepingServDto> housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);
            Assert.listOnlyOne(housekeepingServDtos, "服务不存在");
            //查询服务默认费用
            String defaultFee = housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(housekeepingServDtos.get(0).getShopId(), housekeepingServDtos.get(0).getServId());
            storeCartPo.setProdName(housekeepingServDtos.get(0).getServName());
            storeCartPo.setPrice(defaultFee);
        } else {
            throw new IllegalArgumentException("当前不支持的 类型 cartType:" + storeCartPo.getCartType());
        }

        int flag = storeCartInnerServiceSMOImpl.saveStoreCart(storeCartPo);

        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
